package com.leet.code.simple_array;

import java.util.*;

/**
 * @description: 1337.矩阵中战斗力最弱的K行
 * @author: WYG
 * @time: 2020/12/31 21:12
 */

public class KWeakestRows {
    public int[] kWeakestRows(int[][] mat, int k) {
        List<Integer> temp = new ArrayList<>();
        int[] res = new int[k];
        for (int i = 0; i < mat[0].length; i++) {
            for (int j = 0; j < mat.length; j++) {
                if (temp.size() == k) {
                    break;
                }
                if (temp.contains(j)) continue;
                if (mat[j][i] == 0) {
                    temp.add(j);
                }
            }
        }
        int index = 0;
        while (temp.size() < k) {
            if (!temp.contains(index)) {
                temp.add(index);
            }
            index++;
        }
        for (int i = 0; i < k; i++) {
            res[i] = temp.get(i);
        }
        return res;
    }

    public int[] KWeakestRows(int[][] mat, int k) {
        int[] list = new int[mat.length];
        int[] result = new int[k];
        for(int i=0;i<mat.length;i++){
            list[i] = count(mat[i])*100+i;
        }
        Arrays.sort(list);
        for(int i=0;i<k;i++){
            result[i] = list[i]%100;
        }
        return result;
    }

    public int count(int[] nums){
        int sum=0;
        for(int n:nums){
            if(n!=1){
                break;
            }
            sum+=n;
        }
        return sum;
    }
}
